<?php

namespace app\admin\controller;
use org\Tree;
class AuthGroup extends base
{
    //前置方法
    protected $beforeActionList = [
        'map' => ['only' => 'index'],
        'auth_rule' => ['only' => 'add,edit'],
		'access_del'=>['only' =>'del'],
    ];

    /**
     * 搜索
     */
    protected function map()
    {
        if ($this->request->isGet()) {
            $data = $this->request->param();
            $this->datas = array();
            if (!empty($data['start_time']) && !empty($data['end_time']) && (strtotime($data['start_time']) < strtotime($data['end_time']))) {
                $this->datas ['create_time'] = [
                    ['>', strtotime($data['start_time'])],
                    ['<', strtotime($data['end_time'])]
                ];
            }
            if (!empty($data['cid'])) {
                $this->datas ['cid'] = $data['cid'];
            }
            if (!empty($data['title'])) {
                $this->datas ['title'] = ['like', '%' . $data['title'] . '%'];
            }
        }
    }

    /**
     * 保存
     */
    public function save()
    {
        if (!$this->request->isPost()) {
            return ajax_info('请求失败');
        }
        $data = $this->request->post();
//        $result = $this->validate($data, 'AuthGroup');
//        if (true !== $result) {
//            return ajax_info($result);
//        }

        $data['rules'] = !empty($data['rules_id']) ? ',' . implode(',', $data['rules_id']) . ',' : '';

        if (!empty($data['id'])) {
            return $this->update($data);
        }

        $res = $this->model->allowField(true)->save($data);
        if ($res) {
            return ajax_info('保存成功', 'y');
        } else {
            return ajax_info('添加失败');
        }
    }


    /*     * **************************权限****************************************** */
    public function auth_rule()
    {
        $rule = model('auth_rule')->select();
		$auth_rule = Tree::Forlayer($rule);
        $this->assign('auth_rule', $auth_rule);
    }

    public function access_edit()
    {
        $admin = model('admin')->field('id,name')->select();
        $auth_group_access = db('auth_group_access')->where('group_id', $this->request->param('id'))->column('uid');
        $auth_group_access = implode(',', $auth_group_access);
        $this->assign([
                'admin' => $admin,
                'auth_group_access' => $auth_group_access,
            ]
        );
        return $this->fetch();
    }

    public function access_save()
    {
        if (!$this->request->isPost()) {
            return ajax_info('请求失败');
        }
		
		$data = $this->request->post();
		
		$delete = db('auth_group_access')->where('group_id',$data['id'])->delete();
		if(!empty($data['uid'])){
			foreach($data['uid'] as $key =>$value){
				$list[$key]['uid'] = $value;
				$list[$key]['group_id'] = $data['id'];
			}
			$res= db('auth_group_access')->insertAll($list);
		}
			
        if (isset($res) || isset($delete)) {
            return ajax_info('保存成功','u');
        } else {
            return ajax_info('保存失败');
        }
    }
	
	public function access_del(){
		$delete = db('auth_group_access')->where('group_id',$this->request->param('id'))->delete();
	}
		
}
